﻿package mortal.game.scene3D.layer3D
{
    import __AS3__.vec.*;
    import com.fyGame.fyMap.*;
    import com.gengine.core.frame.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.shader.*;
    import mortal.common.global.*;
    import mortal.game.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.layer3D.model.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.util.*;

    public class MapLayer3D extends SLayer3D
    {
        private var _mapRect:MapRect;
        private var _drawRect:MapRect;
        private var _pieceBitmap:MapPiece3DDictionary;
        protected var mapType:String = "";
        protected var _mapPieceDic:Dictionary;
        private var _loadLevel:int;
        private var _temp0:Vector3D;
        public var curMap3dList:Vector.<MapBitmap3D>;

        public function MapLayer3D(param1:String, param2:int)
        {
            this._pieceBitmap = new MapPiece3DDictionary();
            this._mapPieceDic = new Dictionary();
            this._temp0 = new Vector3D();
            this.curMap3dList = new Vector.<MapBitmap3D>;
            super("");
            this._mapRect = new MapRect();
            this._drawRect = new MapRect();
            this.mapType = param1;
            this._loadLevel = param2;
            this.scaleY = -1;
            this.rotationX = Scene3DUtil._cameraAngle;
            var _loc_3:Number = 10000;
            this.y = (-_loc_3) * Scene3DUtil.cameraAngleSin;
            this.z = _loc_3 * Scene3DUtil.cameraAngleCos;
            return;
        }// end function

        public function reUpload() : void
        {
            var _loc_2:MapBitmap3D = null;
            MapBitmap3DRender.instance.reUpload();
            var _loc_1:* = this._pieceBitmap.map;
            for each (_loc_2 in _loc_1)
            {
                
            }
            return;
        }// end function

        public function drawImp() : void
        {
            if (this.curMap3dList.length > 0)
            {
                MapBitmap3DRender.instance.draw(this.curMap3dList);
            }
            return;
        }// end function

        public function loadMapPiece(param1:Rectangle, param2:Rectangle) : void
        {
            this._mapRect.rect = param1;
            this._drawRect.rect = param2;
            this._mapRect.update(this.loadPiece, this.unLoadPiece);
            this._drawRect.update(this.addPiece, this.removePiece);
            return;
        }// end function

        override public function update(param1:int = 0) : void
        {
            return;
        }// end function

        override public function draw(param1:Boolean = true, param2:ShaderBase = null) : void
        {
            return;
        }// end function

        private function isNoNeedDraw(param1:int, param2:int) : Boolean
        {
            return false;
        }// end function

        public function removeMap3dFromStage(param1:MapBitmap3D) : void
        {
            var _loc_2:* = this.curMap3dList.indexOf(param1);
            if (_loc_2 != -1)
            {
                this.curMap3dList.splice(_loc_2, 1);
            }
            param1.parent = null;
            return;
        }// end function

        public function addMap3dToStage(param1:MapBitmap3D) : void
        {
            var _loc_2:* = this.curMap3dList.indexOf(param1);
            if (_loc_2 == -1)
            {
                this.curMap3dList.push(param1);
                param1.parent = this;
            }
            return;
        }// end function

        private function addPiece(param1:int, param2:int) : void
        {
            var _loc_4:MapImgInfo = null;
            var _loc_5:Object = null;
            var _loc_3:* = this._pieceBitmap.getPieceBitmap(MapFileUtil.mapScene, param1, param2, this.mapType);
            if (!_loc_3)
            {
                _loc_3 = this._pieceBitmap.getMapBitmap();
                _loc_4 = this.getImageInfo(Game.mapInfo, param1, param2) as MapImgInfo;
                _loc_5 = {x:param1, y:param2};
                _loc_4.extData = _loc_5;
                _loc_3.info = _loc_4;
                this._pieceBitmap.addPiece(MapFileUtil.mapScene, param1, param2, _loc_3, this.mapType);
            }
            if (_loc_3)
            {
                if (!this.isNoNeedDraw(param1, param2))
                {
                    if (!this._mapPieceDic[param1 * 1000 + param2])
                    {
                        this.addMap3dToStage(_loc_3);
                        this._mapPieceDic[param1 * 1000 + param2] = _loc_3;
                    }
                }
                else
                {
                    this.removeMap3dFromStage(_loc_3);
                    delete this._mapPieceDic[param1 * 1000 + param2];
                }
            }
            return;
        }// end function

        private function removePiece(param1:int, param2:int) : void
        {
            var _loc_3:* = this._pieceBitmap.getPieceBitmap(MapFileUtil.mapScene, param1, param2, this.mapType);
            if (_loc_3)
            {
                this.removeMap3dFromStage(_loc_3);
                delete this._mapPieceDic[param1 * 1000 + param2];
            }
            return;
        }// end function

        private function loadPiece(param1:int, param2:int) : void
        {
            var _loc_4:Object = null;
            var _loc_5:ResourceInfo = null;
            if (this.isNoNeedDraw(param1, param2))
            {
                return;
            }
            var _loc_3:* = this._pieceBitmap.getPieceBitmap(MapFileUtil.mapScene, param1, param2, this.mapType);
            if (_loc_3 == null)
            {
                _loc_4 = {x:param1, y:param2};
                _loc_5 = this.getImageInfo(Game.mapInfo, param1, param2);
                if (_loc_5)
                {
                    LoaderManager.instance.loadInfo(_loc_5, this.onLoaded, this._loadLevel, _loc_4);
                }
            }
            else
            {
                _loc_3.disposeTime = -1;
            }
            return;
        }// end function

        private function unLoadPiece(param1:int, param2:int) : void
        {
            var _loc_3:* = this._pieceBitmap.getPieceBitmap(MapFileUtil.mapScene, param1, param2, this.mapType);
            if (_loc_3)
            {
                _loc_3.disposeTime = FrameManager.flashFrame.getTime() + MapBitmap3D.DisposeTime;
            }
            var _loc_4:* = this.getImageInfo(Game.mapInfo, param1, param2);
            if (!_loc_4.isLoaded)
            {
                LoaderManager.instance.clearLoadFileByInfo(_loc_4);
                if (_loc_3)
                {
                    this.removeMap3dFromStage(_loc_3);
                    delete this._mapPieceDic[param1 * 1000 + param2];
                }
                this._pieceBitmap.removePiece(MapFileUtil.mapScene, param1, param2, this.mapType, true);
            }
            return;
        }// end function

        protected function getFileName(param1:int, param2:int) : String
        {
            if (this.mapType == MapLayerType.MAPFAR)
            {
                return MapFileUtil.getPieceFarPath(param1, param2);
            }
            return MapFileUtil.getPiecePath(param1, param2);
        }// end function

        private function getImageInfo(param1:FyMapInfo, param2:int, param3:int) : ResourceInfo
        {
            var _loc_8:MapImgInfo = null;
            var _loc_9:String = null;
            var _loc_10:GMapInfo = null;
            var _loc_4:* = this.getFileName(param2, param3);
            var _loc_5:String = ".abc";
            var _loc_6:String = "/";
            if (ParamsConst.instance.isUseATF)
            {
                _loc_5 = _loc_4.substr(_loc_4.length - 5, 5);
                _loc_6 = "_cmp/";
            }
            var _loc_7:* = ResourceManager.getInfoByName(_loc_4);
            if (_loc_7)
            {
                return _loc_7;
            }
            if (isInMapByTitle(param2, param3))
            {
                _loc_8 = new MapImgInfo();
                _loc_8.name = _loc_4;
                _loc_8.mapID = MapFileUtil.mapScene;
                _loc_9 = _loc_8.mapID + _loc_6 + _loc_8.name;
                _loc_10 = GameMapConfig.instance.getMapInfo(_loc_8.mapID);
                if (this.mapType == MapLayerType.MAPFAR)
                {
                    _loc_8.loaclPath = PathConst.mapBgLocalPath + _loc_9;
                    _loc_8.time = _loc_10.bgInfo.version;
                    _loc_8.path = PathConst.mapBgLocalPath + _loc_10.bgInfo.mapName + _loc_6 + _loc_8.name + "?v=" + _loc_8.time;
                }
                else
                {
                    _loc_8.loaclPath = PathConst.mapLocalPath + _loc_9;
                    _loc_8.time = _loc_10.version;
                    _loc_8.path = PathConst.mapPath + _loc_9 + "?v=" + _loc_8.time;
                }
                _loc_8.type = _loc_5;
                ResourceManager.addResource(_loc_8);
                return _loc_8;
            }
            return null;
        }// end function

        private function onLoaded(param1:MapImgInfo = null) : void
        {
            if (param1 == null || param1.mapID != MapFileUtil.mapScene || !param1.extData)
            {
                return;
            }
            var _loc_2:* = param1.extData.x;
            var _loc_3:* = param1.extData.y;
            var _loc_4:* = this._pieceBitmap.getPieceBitmap(MapFileUtil.mapScene, _loc_2, _loc_3, this.mapType);
            if (_loc_4 == null)
            {
                _loc_4 = this._pieceBitmap.getMapBitmap();
                this._pieceBitmap.addPiece(MapFileUtil.mapScene, _loc_2, _loc_3, _loc_4, this.mapType);
            }
            _loc_4.info = param1;
            _loc_4.disposeTime = -1;
            if (this._drawRect.isInRect(_loc_2, _loc_3))
            {
                this.addPiece(_loc_2, _loc_3);
            }
            return;
        }// end function

        public function clearMap() : void
        {
            this._pieceBitmap.clearMap();
            this._mapPieceDic = new Dictionary();
            this._mapRect.unloadAllPiece(this.unLoadPiece);
            this._drawRect.dispose();
            this._mapRect.dispose();
            return;
        }// end function

        public function disposeBitmap() : void
        {
            this._pieceBitmap.disposeBitmap();
            return;
        }// end function

        public static function isInMapByTitle(param1:int, param2:int) : Boolean
        {
            if (param1 < 0 || param1 > Game.mapInfo.gridXNum)
            {
                return false;
            }
            if (param2 < 0 || param2 > Game.mapInfo.gridYNum)
            {
                return false;
            }
            return true;
        }// end function

    }
}

class MapRect extends Object
{
    private var _currentRect:Rect;
    private var _tempRect:Rect;

    function MapRect()
    {
        this._tempRect = new Rect();
        return;
    }// end function

    public function set rect(param1:Rectangle) : void
    {
        this._tempRect.left = param1.left;
        this._tempRect.right = param1.right;
        this._tempRect.top = param1.top;
        this._tempRect.bottom = param1.bottom;
        return;
    }// end function

    public function isInRect(param1:int, param2:int) : Boolean
    {
        return this._tempRect.contains(param1, param2);
    }// end function

    public function update(param1:Function, param2:Function) : void
    {
        var _loc_3:int = 0;
        var _loc_4:int = 0;
        if (this._currentRect == null)
        {
            _loc_3 = this._tempRect.left;
            while (_loc_3 <= this._tempRect.right)
            {
                
                _loc_4 = this._tempRect.top;
                while (_loc_4 <= this._tempRect.bottom)
                {
                    
                    if (param1 != null)
                    {
                        this.param1(_loc_3, _loc_4);
                    }
                    _loc_4++;
                }
                _loc_3++;
            }
            this._currentRect = new Rect();
            this._currentRect.copy(this._tempRect);
        }
        else
        {
            this.updateImpl(param1, param2);
        }
        return;
    }// end function

    private function updateImpl(param1:Function, param2:Function) : void
    {
        var _loc_5:int = 0;
        var _loc_6:int = 0;
        var _loc_7:int = 0;
        if (this._currentRect.equals(this._tempRect))
        {
            return;
        }
        var _loc_3:* = this._currentRect.intersection(this._tempRect);
        var _loc_4:* = _loc_3.isEmpty;
        if (param2 != null)
        {
            _loc_5 = this._currentRect.left;
            while (_loc_5 <= this._currentRect.right)
            {
                
                _loc_6 = this._currentRect.top;
                while (_loc_6 <= this._currentRect.bottom)
                {
                    
                    if (_loc_3.contains(_loc_5, _loc_6) == false)
                    {
                        this.param2(_loc_5, _loc_6);
                    }
                    _loc_6++;
                }
                _loc_5++;
            }
        }
        if (param1 != null)
        {
            _loc_5 = this._tempRect.left;
            while (_loc_5 <= this._tempRect.right)
            {
                
                _loc_6 = this._tempRect.top;
                while (_loc_6 <= this._tempRect.bottom)
                {
                    
                    if (_loc_4 || _loc_3.contains(_loc_5, _loc_6) == false)
                    {
                        this.param1(_loc_5, _loc_6);
                    }
                    _loc_6++;
                }
                _loc_5++;
            }
        }
        this._currentRect.copy(this._tempRect);
        return;
    }// end function

    public function unloadAllPiece(param1:Function) : void
    {
        var _loc_3:int = 0;
        if (this._tempRect == null || this._tempRect.isEmpty)
        {
            return;
        }
        var _loc_2:* = this._tempRect.left;
        while (_loc_2 <= this._tempRect.right)
        {
            
            _loc_3 = this._tempRect.top;
            while (_loc_3 <= this._tempRect.bottom)
            {
                
                this.param1(_loc_2, _loc_3);
                _loc_3++;
            }
            _loc_2++;
        }
        this.dispose();
        return;
    }// end function

    public function isNeedAdd(param1:int, param2:int) : Boolean
    {
        return this._tempRect.contains(param1, param2);
    }// end function

    public function get isNoNeedLoad() : Boolean
    {
        return this._currentRect.containsRect(this._tempRect);
    }// end function

    public function dispose() : void
    {
        this._currentRect = null;
        this._tempRect.setEmpty();
        return;
    }// end function

}

